package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.NewFragmentAdapter;
import com.dingwei.lesyndrome.adapter.SelectedMerchantsAdapter;
import com.dingwei.lesyndrome.bean.NewsListBean;
import com.dingwei.lesyndrome.bean.SelectedMerchantsBean;
import com.dingwei.lesyndrome.mvp.presenter.NewsSearchListPresenter;
import com.dingwei.lesyndrome.mvp.presenter.PeripheralSearchListPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.weight.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/3/7.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class PeripheralSearchListActivity extends BaseActivity implements Contract.PeripheralSearchListView {

    PeripheralSearchListPresenter peripheralSearchListPresenter = new PeripheralSearchListPresenter(this);
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;
    private SelectedMerchantsAdapter adapter;
    String keywords;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_news_search_list_activity;
    }

    @Override
    protected void initData() {
        page = 1;
        peripheralSearchListPresenter.initData(keywords, "zxfb", page, true, true);
    }

    @Override
    protected void initView() {
        keywords = getIntent().getStringExtra("keywords");
        adapter = new SelectedMerchantsAdapter(BaseActivity.context);
        GridLayoutManager layoutManager = new GridLayoutManager(BaseActivity.context, 2);
        layoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(2));
        SpaceDecoration itemDecoration = new SpaceDecoration((int) DisplayUtil.dip2px(BaseActivity.context, 16));//参数是距离宽度
        itemDecoration.setPaddingEdgeSide(true);//是否为左右2边添加padding.默认true.
        itemDecoration.setPaddingStart(true);//是否在给第一行的item添加上padding(不包含header).默认true.
        itemDecoration.setPaddingHeaderFooter(false);//是否对Header于Footer有效,默认false.

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(itemDecoration);


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                peripheralSearchListPresenter.initData(keywords, "zxfb", page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                peripheralSearchListPresenter.initData(keywords, "zxfb", page, true, false);
            }
        });



        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                SelectedMerchantsBean.DataBean.ListBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, PeripheralDetailActivity.class);
                intent.putExtra("id", bean.id);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void onLoadMore(SelectedMerchantsBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onRefresh(SelectedMerchantsBean bean) {

        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData("success");

    }

    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.no_net_btn)
    public void onViewClicked() {
        page = 1;
        peripheralSearchListPresenter.initData(keywords, "zxfb", page, true, true);
    }
}
